﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入模式：1-发送者 2-接收者");
            switch (Console.ReadLine())
            {
                case "1":
                    {
                        Console.Write("请输入测试文件路径:");
                        var filePath = Console.ReadLine();
                        if (!File.Exists(filePath))
                        {
                            Console.WriteLine("文件不存在");
                            return;
                        }
                        var content = File.ReadAllText(filePath, Encoding.UTF8);
                        Console.WriteLine("以下为原文件内容：");
                        Console.WriteLine(content);
                        Console.WriteLine();
                        var sm2 = Vive.Crypto.CryptoFactory.CreateAsymmetric(Vive.Crypto.AsymmetricProviderType.SM2);
                        var key = sm2.CreateKey();
                        var publicKey = key.PublickKey;
                        var privateKey = key.PrivateKey;
                        Console.WriteLine($"SM2公钥：{publicKey}");
                        Console.WriteLine($"SM2私钥：{privateKey}");
                        Console.Write("输入接收方IP:");
                        var epStr = Console.ReadLine();
                        var success = IPEndPoint.TryParse(epStr, out var ip);
                        if (!success)
                        {
                            Console.WriteLine("输入的不是合法IP");
                            return;
                        }
                        var client = new UdpClient();
                        try
                        {
                            client.Connect(ip);
                        }
                        catch
                        {
                            Console.WriteLine("未能建立连接");
                            return;
                        }
                        var sm2Res = sm2.Encrypt(content, publicKey);
                        var data = Encoding.UTF8.GetBytes($"{privateKey}|{sm2Res}");
                        client.Send(data, data.Length);
                        Console.WriteLine("公钥及加密数据已发送");
                        break;
                    }
                case "2":
                    {
                        Console.Write("请输入监听IP：");
                        var epStr = Console.ReadLine();
                        var success = IPEndPoint.TryParse(epStr, out var ip);
                        
                        if (!success)
                        {
                            Console.WriteLine("输入的不是合法IP");
                            return;
                        }
                        var client = new UdpClient(ip);
                        Console.WriteLine("正在等待数据...");
                        var data = client.Receive(ref ip);
                        var dataArr = Encoding.UTF8.GetString(data).Split('|');
                        var pubkey = dataArr[0];
                        var encrypted = dataArr[1];
                        Console.WriteLine($"公钥：{pubkey}");
                        Console.WriteLine("以下为接收到的密文：");
                        Console.WriteLine(encrypted);
                        Console.WriteLine();
                        var sm2 = Vive.Crypto.CryptoFactory.CreateAsymmetric(Vive.Crypto.AsymmetricProviderType.SM2);
                        Console.WriteLine("解密结果：");
                        Console.WriteLine(sm2.Decrypt(encrypted, pubkey));
                        break;
                    }
            }
            
        }
    }
}
